from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
#导入并拆分数据集
dataset = load_iris()
x,y = dataset.data[:,2:4],dataset.target
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=0,test_size=50)
#训练模型
model = RandomForestClassifier(n_estimators=10,random_state=0)
model.fit(x_train,y_train)
#评估模型
pred = model.predict(x_test)
ac = accuracy_score(y_test,pred)
print('随机森林模型预测准确率：',ac)